package com.heifeng.cloudshelf.mvp.programme.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import androidx.recyclerview.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.heifeng.cloudshelf.NetWorkReceiver;
import com.heifeng.cloudshelf.R;
import com.heifeng.cloudshelf.base.BaseActivity;
import com.heifeng.cloudshelf.event.DialogCaneclEvent;
import com.heifeng.cloudshelf.event.DialogDismissEvent;
import com.heifeng.cloudshelf.event.DialogFinishEvent;
import com.heifeng.cloudshelf.event.DialogSpecTextEvent;
import com.heifeng.cloudshelf.event.MoreClickEvent;
import com.heifeng.cloudshelf.event.OpenDetailEvent;
import com.heifeng.cloudshelf.event.SpecTextEvent;
import com.heifeng.cloudshelf.mvp.programme.contract.ProgrammeActivityContract;
import com.heifeng.cloudshelf.mvp.programme.presenter.ProgrammeActivityPresenter;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;


/*
 *方案库、全屋定制、橱柜定制、木门定制、成品宅配  共用界面
 */
public class ProgrammeActivity extends BaseActivity<ProgrammeActivityPresenter> implements ProgrammeActivityContract.View {
    @BindView(R.id.l_error)
    View lError;//网络错误

    @BindView(R.id.l_no_data)
    View lNoData;//没有数据

    @BindView(R.id.r_top)
    RelativeLayout rTop;

    @BindView(R.id.recyclerview)
    RecyclerView recyclerView1;

    @BindView(R.id.rv_bottom)
    RecyclerView recyclerView;

    @BindView(R.id.l_back)
    LinearLayout lBack;

    @BindView(R.id.r_layout)
    RelativeLayout rLayout;

    @BindView(R.id.iv_icon)
    ImageView ivIcon;

    @BindView(R.id.tv_title)
    TextView tvTitle;

    @BindView(R.id.tv_list_count)
    TextView tv_list_count;

    private TranslateAnimation translateAnimation;

    private TranslateAnimation translateAnimation1;

    private TranslateAnimation translateAnimation2;

    private TranslateAnimation translateAnimationBelow;

    private TranslateAnimation translateAnimationAfter;

    private TranslateAnimation translateAnimationOpenDetail;

    private int icon;

    private String title;

    private int type = 0;//0方案库、1全屋定制、2橱柜定制、3木门定制、4成品宅配
    private int templateId = 0;
    private String listCount = "0个方案结果";


    public static void StartActivity(Activity activity, int icon, String title, int type, int templateId) {
        Intent intent = new Intent(activity, ProgrammeActivity.class);
        intent.putExtra("icon", icon);
        intent.putExtra("title", title);
        intent.putExtra("type", type);
        intent.putExtra("templateId", templateId);
        Log.e("templateId", "StartActivity templateId:" + templateId);
        activity.startActivity(intent);
    }

    public static void StartActivity(Activity activity, int icon, String title, int type) {
        Intent intent = new Intent(activity, ProgrammeActivity.class);
        intent.putExtra("icon", icon);
        intent.putExtra("title", title);
        intent.putExtra("type", type);
        activity.startActivity(intent);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_programme;
    }

    @Override
    protected void initEventAndData() {
        EventBus.getDefault().register(this);
        icon = getIntent().getIntExtra("icon", R.mipmap.ic_title_programee);
        title = getIntent().getStringExtra("title");
        type = getIntent().getIntExtra("type", 0);
        templateId = getIntent().getIntExtra("templateId", 0);
        Log.e("templateId", "templateId:" + templateId);

        lError.findViewById(R.id.tv_refresh).setOnClickListener(view -> {
            checkNet();
            showNetError(isNetConnect());
            if (0 != type) {
                mPresenter.getSpecList(templateId);
                if (2 != type) {
                    mPresenter.toSearchBySpecOption(templateId);
                }
            } else {
                mPresenter.getTableModelHouse();
            }
        });

        ivIcon.setImageResource(icon);
        tvTitle.setText(title);

        onStartAnim();
        mPresenter.init();

        recyclerView1.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    Log.e("onScrolled", "newState：" + newState);
                    if (mPresenter.isVisBottom(recyclerView)) {
                        Log.e("onScrolled", "isVisBottom：true");
                        mPresenter.nextPage(templateId);
                    }
                }
            }
        });


        if (0 != type) {
            mPresenter.getSpecList(templateId);
            if (2 != type) {
                mPresenter.toSearchBySpecOption(templateId);
            }
        } else {
            mPresenter.getTableModelHouse();
        }
//        if (0 != type) {
//            mPresenter.getSpecList(templateId);
//            mPresenter.toSearchBySpecOption(templateId);
//        } else {
//            mPresenter.getTableModelHouse();
//        }

        showNetError(isNetConnect());

        NetWorkReceiver.setOnNetConnectListener(isConnected -> showNetError(isConnected));

    }

    private void showNetError(boolean netConnect) {
        if (null != lError) {
            if (netConnect) {//进入界面获取网络状态
                if (View.VISIBLE == lError.getVisibility()) {
                    lError.setVisibility(View.GONE);
                    recyclerView.setVisibility(View.VISIBLE);
                    recyclerView1.setVisibility(View.VISIBLE);
                    tv_list_count.setText(listCount);
                }
            } else {
                if (View.GONE == lError.getVisibility()) {
                    listCount = tv_list_count.getText().toString();
                    lError.setVisibility(View.VISIBLE);
                    recyclerView.setVisibility(View.GONE);
                    recyclerView1.setVisibility(View.GONE);
                    setListSize(0);
                }
            }
        }
    }

    @Override
    public int getTemplateId() {
        return templateId;
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
//        Glide.get(this).clearMemory();
        super.onDestroy();
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    public RecyclerView getRecyclerView1() {
        return recyclerView1;
    }

    @Override
    public void onStartAnim() {
        translateAnimation = (TranslateAnimation) AnimationUtils.loadAnimation(ProgrammeActivity.this, R.anim.anim_bottom_to_top);
        translateAnimation1 = (TranslateAnimation) AnimationUtils.loadAnimation(ProgrammeActivity.this, R.anim.anim_bottom_to_top_slow);
        translateAnimation2 = (TranslateAnimation) AnimationUtils.loadAnimation(ProgrammeActivity.this, R.anim.anim_bottom_to_top_more_slow);

        rTop.startAnimation(translateAnimation);
        recyclerView1.startAnimation(translateAnimation1);
        recyclerView.startAnimation(translateAnimation2);
        lBack.startAnimation(translateAnimation2);
    }

    @Override
    public RecyclerView getRecyclerView() {
        return recyclerView;
    }

    @Override
    public void onBelowAnim() {
        translateAnimationBelow = (TranslateAnimation) AnimationUtils.loadAnimation(ProgrammeActivity.this, R.anim.anim_programme_below);
        translateAnimationBelow.setFillAfter(true);
        rLayout.startAnimation(translateAnimationBelow);
    }

    @Override
    public void onResumeAnim() {
        translateAnimationAfter = (TranslateAnimation) AnimationUtils.loadAnimation(ProgrammeActivity.this, R.anim.anim_programme_after);
        rLayout.startAnimation(translateAnimationAfter);
    }

    @Override
    public void onOpenDetailAnim() {
        translateAnimationOpenDetail = (TranslateAnimation) AnimationUtils.loadAnimation(ProgrammeActivity.this, R.anim.anim_open_detail);
        rLayout.startAnimation(translateAnimationOpenDetail);
    }

    @Override
    public LinearLayout getLBack() {
        return lBack;
    }

    @Override
    public int getType() {
        return type;
    }

    @Override
    public void setListSize(int count) {
        tv_list_count.setText(count + "个方案结果");
    }

    @Override
    public void showError(boolean isNull) {
        if (isNull) {
            lNoData.setVisibility(View.VISIBLE);
        } else {
            lNoData.setVisibility(View.INVISIBLE);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void LoadMoreEvent(MoreClickEvent event) {
        //onBelowAnim();

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void DialogDismissEvent(DialogDismissEvent event) {
        //onResumeAnim();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void DialogCaneclEvent(DialogCaneclEvent event) {
        //onResumeAnim();
        Log.e("TAGS", "DialogDismissEvent");
        if (event.isShowed()) {
            mPresenter.keepSelectSpSpec(); //刷新选项
        } else {
            mPresenter.setOpenSpSpecList(true,event.isChanged()); //刷新选项
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void OpenDetailEvent(OpenDetailEvent event) {
        onOpenDetailAnim();

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                ProgrammeDetailActivity.open(ProgrammeActivity.this, event.getObject());
            }
        }, 400);    //延时
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void OpenDetailEvent(SpecTextEvent event) {
        //未弹窗的选中
//        Log.e("SpecEvent", "SpecTextEvent clicked:" + event.getTitle() + ",i:" + event.getPosition() + ",id:" + event.getId() + ",p:" + event.getParentId());
        mPresenter.setPageIndex(1);
        mPresenter.putSpecTextsInGroup(event.getPosition(), event.getId(), null);
        if (0 != type) {
            mPresenter.setSpecList(event.getPosition(), event.getId(), event.getParentId());
            mPresenter.toSearchBySpecOption(templateId);
        } else {
            mPresenter.getTableModelHouse();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void OpenDetailEvent(DialogSpecTextEvent event) {
        //弹窗的选中
        Log.e("SpecEvent", "DialogSpecTextEvent clicked:" + event.getTitle() + ",i:" + event.getPosition() + ",id:" + event.getId());
        mPresenter.putSpecTextsInGroup(event.getPosition(), event.getId(), event);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void OpenDetailEvent(DialogFinishEvent event) {
        //弹窗的确定
        Log.e("SpecTextEvent", "DialogFinishEvent clicked");
        mPresenter.setPageIndex(1);
        if (0 != type) {
            mPresenter.toSearchBySpecOption(templateId);
        } else {
            mPresenter.getTableModelHouse();
        }
        mPresenter.setOpenSpSpecList(false,true); //刷新选项
    }

    @OnClick({R.id.l_back})
    public void Onclick(View view) {
        switch (view.getId()) {
            case R.id.l_back:
                finish();
                break;
        }
    }
}
